package com.qfedu.knowledge.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName PublisherConfirmAndReturnConfig
 * @Author ykx
 * @Date 2024/5/6 11:52
 * @Version 1.0
 */
@Component
@Slf4j
public class PublisherConfirmAndReturnConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct  // 这个注解在当前类创建好对象后执行
    public void initMethod() {
        rabbitTemplate.setReturnsCallback(this);
        rabbitTemplate.setConfirmCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            log.error("消息已经送达到Exchange");
            log.error("correlationData = " + correlationData);
            log.error("s = " + s);
        } else {
            log.error("消息没有送达到Exchange");
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        log.error("消息没有送达到Queue");
    }

}
